<?php
require_once APP . '/application/controllers/UserAction.php';
require_once(APP . '/application/models/MeetModel.php');
require_once(APP . '/application/models/Article.php');
require_once(APP . '/application/models/MarticleModel.php');
require_once(APP . '/application/models/UserModel.php');

/**
 * 会议管理主页
 * @package admin
 */
class MeetcpController extends UserAction
{
	/**
	 * 
	 * @var MeetModel
	 */
    private $obj_meet;

    /**
     * 会议信息
     * @var Array
     */
    private $minfo;
    
    /**
     * 会议编号
     * @var Integer
     */
    private $mid;
    
    public function init()
    {
    	$mid = (int)$this->getParam("mid");
    	$uid = (int)$_SESSION['uid'];
    	
    	if ($mid != 0 && $uid != 0){

    		$this->obj_meet = new MeetModel($this->db);
    		 
    		$this->minfo = $this->obj_meet->show($mid);
    		
    		if (empty($this->minfo)){
    			exit("该会议不是你创建的！");
    		}
    		
    		$this->view->minfo = $this->minfo;
    		$this->view->mid = $mid;
    	}
    	
    	$this->mid = $mid;
    }

    /**
     * 创建会议第0步 
     */
    public function createAction()
    {
    	$time = (int)$this->db->fetchOne("select value from platset where name='plat_time'");
    	
    	$aid = (int)$this->db->fetchOne("select value from platset where name='plat_hyxz'");
    	$content = $this->db->fetchOne("select content from article where aid={$aid}");
    	
    	$this->view->htitle = '创建会议须知和免责条款';
    	$this->view->time = $time;
    	$this->view->content = $content;
    	
    	$this->render("create");
    }
    
    
    /**
     * 会议创建者首页
     *
     */
    public function indexAction()
    {
    	$nowp = (int)$this->getParam("nowp");
    	$per_page = 8;
    	
    	$obj_meet = new MeetModel($this->db);
    	
    	$result = $obj_meet->fetchAllByUid($this->uid, $nowp, $per_page);

    	foreach($result['list'] as $k => $v){
    		$result['list'][$k]['url'] = $this->view->fullUrl . '/' . $v['domain'];
    	}
    	
    	$this->view->list = $result['list'];
    	$this->view->page = $result['page'];
    	$this->view->pagenum = Util::pageNumStyle($result['page']);
    	
    	$this->render("index");
    }
    
    
    /**
     * 创建会议 第一步
     *
     */
    public function create1Action()
    {    	
    	$this->view->content = $this->minfo;
    	
    	$this->view->htitle = "注册登录";
    	$this->view->css_files = array("js/jquery-ui/jquery-ui-1.8.16.custom.css");
    	
    	$this->render("create1");
    }
    
    
    /**
     * 创建会议
     *
     */
    public function docreate1Action()
    {
    	$param = $this->getAllParams();
    	$mid = (int)$this->getParam("mid");
    	 
    	foreach ($param as $index => $v){
    		$param[$index] = trim($v);
    	}
    	 
    	$param['domain'] = strtolower($param['domain']);
    
    	if (empty($param['name'])){
    		$this->showmessage("名称不能为空！");
    	}
    	 
    	if (empty($param['domain'])){
    		$this->showmessage("域名不能为空");
    	}
    	
    	require_once(APP . '/application/models/MeetModel.php');
    	 
    	$obj_meet = new MeetModel($this->db);
    	 
    	if (!ereg("^([a-zA-Z0-9_-])+$",$param['domain']))
    		$this->showmessage("域名只能是英文字母，数字，下划线组成！");
    	
    	if (strlen($param['domain']) < 3){
    		$this->showmessage("域名至少3个字符！");
    	}
    	 
    	// 修改
    	if ($mid > 0){
    		$content = $obj_meet->show($mid);
    		
    		if ($content['status'] == 3)
    			$this->showmessage("会议已通过审核，不能修改！");
    		
    		if ($content['domain'] !=$param['domain'] && $obj_meet->isExistDomain($param['domain'])){
    			$this->showmessage("域名已存在，请更换域名！");
    		}
    		
    		try{
    			$obj_meet->change($mid, $param);
    			$result = array("status" => 1, "mid" => $mid);
    		}catch(Exception $e){
    			$result = array("status" => 0, "message" => "修改失败！请重试！" . $e->getMessage());
    		}
    		echo json_encode($result);
    		return;
    	}
    	
    	if ($obj_meet->isExistDomain($param['domain'])){
    		$this->showmessage("域名已存在，请更换域名！");
    	}
    	 
    	try{
    		$param['createtime'] = date("Y-m-d H:i:s");
    		$param['uid'] = $this->uid;
    		$param['style'] = 0;
    		$param['image'] = '';
    		$param['top'] = '';
    		$param['left'] = '';
    		$param['regitems'] = '';
    		$param['enregitems'] = '';
    		$param['status'] = 0;
    		$param['public'] = 0;
    		$param['del'] = 0;
    		$param['status'] = 0;
    
    		$mid = $obj_meet->add($param);
    		$result = array("status" => 1, 'mid' => $mid);
    	}catch (Exception $e){
    		$result = array("status" => 0, "message" => "创建失败！请重试！" . $e->getMessage());
    	}
    	 
    	echo json_encode($result);
    }
    
    
    /**
     * 风格设置
     *
     */
    public function create2Action()
    {
    	$mid = $this->getParam("mid");
    	$obj_meet = new MeetModel($this->db);
    	
    	if ($this->getParam("style") != null){
    		$obj_meet->change($mid, array("style" => (int) $this->getParam("style")));
    		
    		echo '{"status":1}';
    		exit;
    		// 修改成功
    	}
    	
    	$content = $this->minfo;
    	$this->view->content = $content;
    	$this->view->htitle = "设置风格";
    	
    	$this->render("create2");
    }
    
    /**
     * 横幅图片设置
     *
     */
    public function create3Action()
    {
    	$mid = $this->getParam("mid");
    	$obj_meet = new MeetModel($this->db);
    	$image = Util::upfile("image", "image", array("png", "jpg", "jpeg", "gif", "swf"));
    	
    	if ($image != ""){
    		//$obj_meet->change($mid, array("image" => $image));
    
    		// 修改成功
    		
    		$this->view->previewimg = $image;
    	}
    	 
    	$this->render("create3");
    }
    
    /**
     * 设置图片
     */
    public function docreate3Action()
    {
    	$mid = $this->getParam("mid");
    	$url = $this->getParam('url');
    	
    	$obj_meet = new MeetModel($this->db);
    	 
    	$obj_meet->change($mid, array("image" => $url));
    	echo '{"status":1}';
    }
    
    
    /**
     * 导航设置
     *
     */
    public function create4Action()
    {
    	$mid = $this->getParam("mid");
    	$key = $this->getParam("key");
    	if (!in_array($key, array("top", "left"))){
    		exit("key error:" . $key);
    	}
    	
    	$obj_meet = new MeetModel($this->db);
    	 
    	$content = $obj_meet->show($mid);
    	    	
    	$content[$key] = @unserialize($content[$key]);
    	
    	if (!is_array($content[$key])){
    		$content[$key] = array();
    	}
    	    
    	$this->view->content = $content;
    	$this->view->key = $key;
    	$this->view->menu = $content[$key];
    	
    	if ($key == 'top'){
    		$this->view->preurl = $this->view->baseUrl . '/?c=meetcp&a=create3&mid=' . $mid;
    		$this->view->nexturl = $this->view->baseUrl . '/?c=meetcp&a=create4&key=left&mid=' . $mid;
    	}else{
    		$this->view->preurl = $this->view->baseUrl . '/?c=meetcp&a=create4&key=top&mid=' . $mid;
    		$this->view->nexturl = $this->view->baseUrl . '/?c=meetcp&a=create5&mid=' . $mid;
    	}
    	
    	$this->render("create4");
    }
    
    
    /**
     * 编辑界面
     */
    public function docreate4_kdeditAction()
    {
    	$aid = (int)$this->getParam("aid");
    	
    	$this->view->aid = $aid;
    	
    	$marticle = new MarticleModel($this->db);
    	$content = $marticle->show($aid);
    	
    	$this->view->acontent = $content['content'];
    	$this->render("docreate4_kdedit");
    }
    
    /**
     * 文章编辑
     */
    public function docreate4_articleAction()
    {
    	if (empty($this->mid)){
    		exit("no mid");
    	}
    	
    	$aid = (int)$this->getParam("aid");
    	$uid = (int)$_SESSION['uid'];
    	$content = $this->getParam('content');
    	
    	$marticle = new MarticleModel($this->db);
    	if (empty($aid)){
    		$value = array("mid" => $this->mid, "uid" => $uid, "content" => $content);
    		$aid = $marticle->add($value);
    	}else{
    		$value = array("content" => $content);
    		$marticle->change($aid, $uid, $value);
    	}
    	
    	$result = array("status" => 1, "aid" => $aid);
    	echo json_encode($result);
    }
    
    
    /**
     * 导航设置
     *
     */
    public function docreate4_addAction()
    {
    	$mid = $this->getParam("mid");
    	$key = $this->getParam("key");
    	$type = $this->getParam("type");
    	$name = $this->getParam("name");
    	$enname = $this->getParam("enname");
    	$show = (int)$this->getParam("show");
    	$aid = $this->getParam("aid");
    	$enaid = $this->getParam("enaid");
    	
    	$obj_meet = new MeetModel($this->db);
    
    	$content = $this->minfo;

    	if ($content['status'] == 3)
    		$this->showmessage("会议已通过审核，不能添加！");
    	 
    	$content[$key] = @unserialize($content[$key]);
    	if (!is_array($content[$key])){
    		$content[$key] = array();
    	}
    	if (count($content[$key]) >= 15){
    		$this->showmessage("添加失败，最多只能添加15项");
    	}
    	
    	$item = array();
    	$item['uuid'] = md5(uniqid(mt_rand(), true));
    	$item['type'] = $type;
    	$item['name'] = trim($name);
    	$item['aid'] = $aid;
    	
    	$item['enname'] = trim($enname);
    	$item['enaid'] =  $enaid;
    	
    	$item['show'] = $show;
    	
    	$content[$key][] = $item;
    	
    	$str_save = serialize($content[$key]);
    	try{
    		$this->obj_meet->change($mid, array($key => $str_save));
    	}catch(Exception $e){
    		$this->showmessage("添加失败，请重试");
    	}
    	
    	echo json_encode(array("status" => 1));
    }
    
    

    /**
     * 导航设置 编辑
     *
     */
    public function docreate4_editAction()
    {
    	$mid = $this->getParam("mid");
    	$seq = (int)$this->getParam("index");
    	$key = $this->getParam("key");
    	
    	$type = $this->getParam("type");
    	$name = $this->getParam("name");
    	$enname = $this->getParam("enname");
    	$show = (int)$this->getParam("show");
    	$aid = $this->getParam("aid");
    	$enaid = $this->getParam("enaid");
    	
    	$content = $this->minfo;
    
    	$content[$key] = @unserialize($content[$key]);
    	if (!is_array($content[$key])){
    		exit("no");
    	}
    	
    	$item = &$content[$key][$seq];
    	
    	if (empty($item)){
    		$this->showmessage("修改失败1");
    	}
    	if ($item['type'] == 'login'){
    		$this->showmessage("修改失败(login)");
    	}
    	
    	// 修改
    	$item['name'] = trim($name);
    	$item['aid'] = $aid;
    	
    	$item['enname'] = trim($enname);
    	$item['enaid'] =  $enaid;
    	
    	$item['show'] = $show;
    	
    	$str_save = serialize($content[$key]);
    	try{
    		$this->obj_meet->change($mid, array($key => $str_save));
    	}catch(Exception $e){
    		$this->showmessage("修改失败，请重试");
    	}

    	echo '{"status":1}';
    }
    
    

    /**
     * 导航设置 删除
     *
     */
    public function docreate4_delAction()
    {
    	$mid = $this->getParam("mid");
    	$seq = (int)$this->getParam("index");
    	$key = $this->getParam("key");
    	$obj_meet = new MeetModel($this->db);    
    	$marticle = new MarticleModel($this->db);
    	
    	$content = $this->minfo;

    	if ($content['status'] == 3)
    		$this->showmessage("会议已通过审核，不能删除！");
    	
    	$content[$key] = @unserialize($content[$key]);
    	$item = $content[$key];
    	
    	if (isset($content[$key][$seq]) && $content[$key][$seq]['type']!='login'){
    		unset($content[$key][$seq]);
    		
    	if (!empty($item['aid'])){
    			$marticle->del($item['aid']);
    		}
    		if (!empty($item['enaid'])){
    			$marticle->del($item['enaid']);
    		}
    		
    		// 重置索引
    		$content[$key] = array_values($content[$key]);
    		
    		$str_save = serialize($content[$key]);
    		try{
    			$this->obj_meet->change($mid, array($key => $str_save));
    		}catch(Exception $e){
    			$this->showmessage("删除失败!");
    		}
    	}
    	
    	echo '{"status":1}';
    }
    
    
    /**
     * 导航设置 上移下移
     *
     */
    public function docreate4_posAction()
    {
    	$mid = $this->getParam("mid");
    	$seq = (int)$this->getParam("index");// 从0开始
    	$opp = $this->getParam("opp");
    	$key = $this->getParam("key");
    	
    	$newseq = $opp=="down"?$seq++ : $seq--;
    	
    	if ($newseq <=0)
    		$newseq = 0;
    	
    	$content = $this->minfo;
    
    	$content[$key] = @unserialize($content[$key]);

    	if ($newseq >= count($content[$key]))
    		$newseq = count($content[$key])-1;
    	
    	if (isset($content[$key][$seq]) && isset($content[$key][$newseq])){
    		// 交换顺序
    		$temp = $content[$key][$seq];
    		$content[$key][$seq] = $content[$key][$newseq];
    		$content[$key][$newseq] = $temp;
    		
    		$content[$key] = array_values($content[$key]);// 重置索引
    		$str_save = serialize($content[$key]);
    		try{
    			$this->obj_meet->change($mid, array($key => $str_save));
    			$result = array("status" => 1, "index" => $seq, "newindex" => $newseq);
    			echo json_encode($result);
    		}catch(Exception $e){
    			$this->showmessage("移动失败，请重试！",0);
    		}
    	}else{
    		$this->showmessage("移动失败1，请重试！", 111);
    	}
    }
    
    
    /**
     * 注册表单设置
     *
     */
    public function create5Action()
    {
    	$mid = $this->getParam("mid");
    	$key = "regitems";
    	 
    	$obj_meet = new MeetModel($this->db);
    
    	$content = $obj_meet->show($mid);
    	 
    	$content[$key] = @unserialize($content[$key]);
    	if (!is_array($content[$key])){
    		$content[$key] = array();
    	}
    
    	$this->view->content = $content;
    	$this->view->items = $content[$key];
    	 
    	$this->render("create5");
    }
    
    
    /**
     * 注册表单设置 添加
     *
     */
    public function create5_addAction()
    {
    	$mid = $this->getParam("mid");
    	$seq = $this->getParam("seq");
    	$content = $this->minfo;
    
    	if ($content['status'] == 3)
    		$this->showmessage("会议已通过审核，不能添加！");
    	
    	$regitems = @unserialize($content['regitems']);
    	
    	if (!is_array($regitems)){
    		$regitems = array();
    	}
    	
    	if ($seq !== NULL){
    		$item = $regitems[$seq];
    	}else{
    	
    		$item = array();
    		$item['uuid'] = md5(uniqid(mt_rand())+microtime(true));
    	}
    	
    	$item['type'] = (int)$this->getParam("type");
    	
    	if ($item['type'] == 7){
    		$item['title'] = "-";
    		$item['entitle'] = '';
    		$item['value'] = '';
    		$item['envalue'] = '';
    		$item['required'] = 0;
    		$item['show'] = 1;
    		$item['beizhu'] = '';
    		$item['enbeizhu'] = '';
    		$item['desc'] = '';
    		$item['endesc'] = '';
    	}elseif($item['type'] == 8){
    		$item['title'] = "-";
    		$item['entitle'] = '';
    		$item['value'] = '';
    		$item['envalue'] = '';
    		$item['required'] = 0;
    		$item['show'] = (int)$this->getParam("show");
    		$item['beizhu'] = '';
    		$item['enbeizhu'] = '';
    		$item['desc'] = $this->getParam("desc");;
    		$item['endesc'] = $this->getParam("endesc");
    	}else{
	    	$item['title'] = $this->getParam("title");
	    	$item['entitle'] = $this->getParam("entitle");
	    	$item['value'] = $this->getParam("value");
	    	$item['envalue'] = $this->getParam("envalue");
	    	$item['required'] = (int)$this->getParam("required");
	    	$item['show'] = (int)$this->getParam("show");
	    	$item['beizhu'] = $this->getParam("beizhu");
	    	$item['enbeizhu'] = $this->getParam("enbeizhu");
	    	$item['desc'] = $this->getParam("desc");
	    	$item['endesc'] = $this->getParam("endesc");
    	}
	
    	if (!empty($item['title'])){
    		if ($seq !== NULL){
    			$regitems[$seq] = $item;
    		}else{
    			$regitems[] = $item;
    		}
    		
    		$str_save = serialize($regitems);
    		try{
    			$this->obj_meet->change($mid, array("regitems" => $str_save));
    		}catch(Exception $e){
    			$this->showmessage("修改失败");
    		}
    		 
    		echo json_encode(array("status" => 1));
    	}else{
    		$this->showmessage("标题不能为空");
    	}
    }
    
    
    
    /**
     * 注册表单设置 上移下移
     *
     */
    public function create5_posAction()
    {
    	$mid = $this->getParam("mid");
    	$seq = (int)$this->getParam("index");
    	$opp = $this->getParam("opp");
    	$key = "regitems";
    	 
    	$newseq = $opp=="down"?$seq++ : $seq--;
    	 
    	if ($newseq <=0)
    		$newseq = 0;
    	     
    	$content = $this->minfo;
    
    	$content[$key] = @unserialize($content[$key]);
    
    	if ($newseq >= count($content[$key]))
    		$newseq = count($content[$key]) - 1;
    	 
    	if (isset($content[$key][$seq]) && isset($content[$key][$newseq])){
    		$temp = $content[$key][$seq];
    		$content[$key][$seq] = $content[$key][$newseq];
    		$content[$key][$newseq] = $temp;
    
    		$content[$key] = array_values($content[$key]);
    		$str_save = serialize($content[$key]);
    		try{
    			$this->obj_meet->change($mid, array($key => $str_save));
    			$result = array("status" => 1, "index" => $seq, "newindex" => $newseq);
    			echo json_encode($result);
    		}catch(Exception $e){
    			$this->showmessage("移动失败0");
    		}
    	}else{
    		$this->showmessage("移动失败1");
    	}
    }
    
    
    /**
     * 导航设置 删除
     *
     */
    public function create5_delAction()
    {
    	$mid = $this->getParam("mid");
    	$seq = (int)$this->getParam("index");
    	$key = "regitems";
    	
    	$content = $this->minfo;

    	if ($content['status'] == 3)
    		$this->showmessage("会议已通过审核，不能删除！");
    	
    	$content[$key] = @unserialize($content[$key]);
    	 
    	if (isset($content[$key][$seq])){
    		unset($content[$key][$seq]);
    
    		// 重置索引
    		$content[$key] = array_values($content[$key]);
    
    		$str_save = serialize($content[$key]);
    		try{
    			$this->obj_meet->change($mid, array($key => $str_save));
    			echo '{"status":1}';
    		}catch(Exception $e){
    			$this->showmessage("删除失败0");
    		}
    	}else{
    		$this->showmessage("删除失败1");
    	}
    }
    
    
    /**
     * 域名检测
     */
    public function domaincheckAction()
    {
    	$domain = $this->getParam("domain");
    	$domain = strtolower(trim($domain));
    	    	
    	$obj_meet = new MeetModel($this->db);
    	
    	if (!ereg("^([a-zA-Z0-9])+",$domain))
    	 	$this->showmessage("域名只能是英文字母，数字组成！");
    	 	
    	if ($obj_meet->isExistDomain($domain)){
    		$this->showmessage("域名已存在，请更换域名！");
    	}
    	
    	echo json_encode(array("status" => 1));
    }
    
    /**
     * 创建结束
     */
	public function create6Action()
	{
		$this->render("create6");
	}
    
    
    /**
     * 修改状态
     */
    public function cstatusAction()
    {
    	$mid = $this->getParam("mid");
    	$uid = (int)$_SESSION['uid'];
    	$newstatus = (int)$this->getParam("newstatus");
    	 
    	$obj_meet = new MeetModel($this->db);
    	 
    	$content = $obj_meet->show($mid);
    	if ($content['uid'] != $uid){
    		$this->showmessage("该会议不是你创建的!");
    	}
    	
    	$obj_meet->change($mid, array("status" => $newstatus, "submittime" => date("Y-m-d H:i:s")));
    	
    	echo json_encode(array("status" => 1));
    }
    
    /**
     * 管理主页
     */
    public function manageAction()
    {
    	$mid = $this->getParam("mid");
    	$uid = (int)$_SESSION['uid'];
    	
    	$obj_meet = new MeetModel($this->db);
    	
    	$content = $obj_meet->show($mid);
    	if ($content['uid'] != $uid){
    		exit("该会议不是你创建的!");
    	}
    	
    	$menu_left = unserialize($content['left']);
    	$menu_top = unserialize($content['top']);
    	if (!is_array($menu_left))$menu_left = array();
    	if (!is_array($menu_top))$menu_top = array();
    	
   		foreach($menu_left as $index => $v){
    		$menu_left[$index]['name'] = $v['name'] . '(左菜单)';
    	}
    	foreach($menu_top as $index => $v){
    		$menu_top[$index]['name'] = $v['name'] . '(顶菜单)';
    	}
    	
    	$menu_all = array_merge($menu_top, $menu_left);
    	$arr_files = array();
    	foreach($menu_all as $v){
    		if ($v['type'] == 'file'){
    			$arr_files[] = $v;
    		}
    	}
    	
    	$this->view->arr_file_item = $arr_files;
    	$this->view->content = $content;
    	$this->render("manage");
    }
    
    /**
     * 管理主页
     */
    public function managemenuAction()
    {
    	$mid = $this->getParam("mid");
    	$uid = (int)$_SESSION['uid'];
    	$type = $this->getParam("type");
    	 
    	$obj_meet = new MeetModel($this->db);
    	 
    	$content = $obj_meet->show($mid);
    	if ($content['uid'] != $uid){
    		exit("该会议不是你创建的!");
    	}
    	
    	if ($type == "m_left"){
    		$user = new UserModel($this->db);
    		$this->view->uinfo = $user->show($uid);
    	}
    	 
    	$this->view->content = $content;
    	$this->render($type);
    }
    
    /**
     * 删除
     */
    public function delAction()
    {
    	$mid = $this->getParam("mid");
    	$uid = (int)$_SESSION['uid'];
    	$page = $this->getParam("page");
    	
    	$obj_meet = new MeetModel($this->db);
    	
    	$content = $obj_meet->show($mid);
    	if ($content['uid'] != $uid){
    		exit("该会议不是你创建的!");
    	}
    	
    	$obj_meet->del($mid);
    	
    	$this->redirectAction("meetcp", "index");
    }
}